package com.desay.pangoo.itmt.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.Data;

/**
 * @author uidq1163 on [2023年01月04日上午10:16:03] <br>
 *         ITMT组织架构信息
 * @Modified By： [修改人] on [修改日期] for [修改说明]
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "org_chart")
@Data
public class OrgChartEntity extends BaseEntity implements Serializable {

	private static final long serialVersionUID = -3161217180941112174L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", columnDefinition = "bigint COMMENT '主键'")
	private Long id;

	@Column(name = "login", nullable = false, columnDefinition = "varchar(255) COMMENT '登录名称'")
	private String login;

	@Column(name = "nickName", nullable = false, columnDefinition = "varchar(255) COMMENT '用户全称'")
	private String nickName;

	@Column(name = "email", nullable = false, columnDefinition = "varchar(255) COMMENT '个人邮箱信息'")
	private String email;

	@Column(name = "avatar", nullable = true, columnDefinition = "mediumtext COMMENT '用户头像'")
	private String avatar;
	@Column(name = "bu", columnDefinition = "varchar(50) COMMENT '所属BU'")
	private String bu;

	/**
	 * 组织角色:ITMT ChairMan:1，ITMT Leader:2，ITMT
	 * SubLeader:3，ITMT成员:4,TSP:5,PRG:6,秘书长：7，委员：8,统筹人：9,PLTMT Leader:10,
	 */
	@Column(name = "org_role", nullable = false, columnDefinition = "int COMMENT '组织角色:ITMT ChairMan:1，ITMT Leader:2，ITMT SubLeader:3，ITMT成员:4，TSP:5,PRG:6'")
	private Integer orgRole;

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent", cascade = { CascadeType.REFRESH, CascadeType.REMOVE,
			CascadeType.MERGE })
	@JsonIgnoreProperties({ "parent" })
	private Set<OrgChartEntity> children = new HashSet<>(16);

	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH }, optional = true)
	@JoinColumn(name = "parent_id")
	@JsonIgnoreProperties({ "children" })
	private OrgChartEntity parent;

	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	@JoinColumn(name = "work_group_id")
	private WorkGroupEntity workGroup;

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		OrgChartEntity other = (OrgChartEntity) obj;
		return Objects.equals(avatar, other.avatar) && Objects.equals(children, other.children)
				&& Objects.equals(email, other.email) && Objects.equals(id, other.id)
				&& Objects.equals(login, other.login) && Objects.equals(nickName, other.nickName)
				&& Objects.equals(orgRole, other.orgRole) && Objects.equals(parent, other.parent)
				&& Objects.equals(workGroup, other.workGroup);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + Objects.hash(avatar, email, id, login, nickName, orgRole);
		return result;
	}
}
